package org.ecloud.redis.config;

import org.ecloud.redis.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfigure {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
    /**
     * Provides the central class of the Redis module for Redis interactions.
     * @return the Redis interaction class
     */
    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    	logger.debug("redis template initial.");
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());//key的序列化适配器  
        
        RedisUtil.redisTemplate = redisTemplate;
        return redisTemplate;
    }
    
    /**
     * Provides the central class of the Redis module for Redis interactions.
     * @return the Redis interaction class
     */
    @Bean
    @ConditionalOnMissingBean(name = "redisTemplateInteger")
    public RedisTemplate<String, Integer> redisTemplateInteger(RedisConnectionFactory factory) {
    	logger.debug("integer redis template initial.");
    	RedisTemplate<String, Integer> redisTemplateInteger = new RedisTemplate<>();
    	redisTemplateInteger.setConnectionFactory(factory);
    	redisTemplateInteger.setKeySerializer(new StringRedisSerializer());//key的序列化适配器  
    	
    	RedisUtil.redisTemplateInteger = redisTemplateInteger;
    	return redisTemplateInteger;
    }
    
    /**
     * Provides the central class of the Redis module for Redis interactions.
     * @return the Redis interaction class
     */
    @Bean
    @ConditionalOnMissingBean(name = "redisTemplateString")
    public RedisTemplate<String, String> redisTemplateString(RedisConnectionFactory factory) {
    	logger.debug("string redis template initial.");
    	RedisTemplate<String, String> redisTemplateString = new RedisTemplate<>();
    	redisTemplateString.setConnectionFactory(factory);
    	redisTemplateString.setKeySerializer(new StringRedisSerializer());//key的序列化适配器  
    	redisTemplateString.setValueSerializer(new StringRedisSerializer());//value的序列化适配器
    	
    	RedisUtil.redisTemplateString = redisTemplateString;
    	return redisTemplateString;
    }
	
}
